import axios from './ajax'
import _g from './global/global'
import Vue from 'vue'
import store from '../store'

const bus = new Vue()

const apiMethods = {
    methods: {
        apiGet: function(url, data) {
            return new Promise((resolve, reject) => {
              _g.openGlobalLoading()
                axios.get(url, data).then((response) => {
                  _g.closeGlobalLoading()
                  if(!response){
                    bus.$message({
                      message: '请求超时，请检查网络',
                      type: 'warning'
                    })
                  }
                    resolve(response)
                }, (response) => {
                    reject(response)
                    _g.closeGlobalLoading()
                    bus.$message({
                        message: '请求超时，请检查网络',
                        type: 'warning'
                    })
                })
            })
        },
        /*
         * 增加文件下载get方法,下载是乱码，暂时不能解决，直接链接替代
         */
        apiGetFile(url, data) {
            return new Promise((resolve, reject) => {
                axios.get(url, data, {
                        responseType: 'stream'
                    })
                    .then((response) => {
                        resolve(response)
                    }, (response) => {
                        reject(response)
                        _g.closeGlobalLoading()
                        bus.$message({
                            message: '请求超时，请检查网络',
                            type: 'warning'
                        })
                    })
            })
        },
        apiPost: function(url, data) {
            return new Promise((resolve, reject) => {
              _g.openGlobalLoading()
                axios.post(url, data).then((response) => {
                  _g.closeGlobalLoading()
                    resolve(response)
                }).catch((response) => {
                    // console.log('f', response)
                  _g.closeGlobalLoading()
                    resolve(response)
                    bus.$message({
                        message: '请求超时，请检查网络',
                        type: 'warning'
                    })
                })
            })
        },
        apiDelete(url, data) {
            return new Promise((resolve, reject) => {
              _g.openGlobalLoading()
                axios.delete(url, data).then((response) => {
                  _g.closeGlobalLoading()
                    resolve(response)
                }, (response) => {
                  _g.closeGlobalLoading()
                    reject(response)
                    _g.closeGlobalLoading()
                    bus.$message({
                        message: '请求超时，请检查网络',
                        type: 'warning'
                    })
                })
            })
        },
        apiPut(url, obj) {
            return new Promise((resolve, reject) => {
              _g.openGlobalLoading()
                axios.post(url, obj).then((response) => {
                  _g.closeGlobalLoading()
                    resolve(response)
                }, (response) => {
                    _g.closeGlobalLoading()
                    bus.$message({
                        message: '请求超时，请检查网络',
                        type: 'warning'
                    })
                    reject(response)
                })
            })
        },
        
    computed: {
        showLoading() {
            // return store.globalLoading
        }
    }
}

}
export default apiMethods
